﻿using System;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using System.IO;
using Color = System.DrawingCore.Color;
using Point = System.DrawingCore.Point;
namespace FrameWork
{
   public class ValidateCode
   {
       public static byte[] CreateValidateGraphic(out String code, int codeLength = 4, int width = 100, int height = 40, int fontSize = 20)
       {
            var sCode = String.Empty;
            //顏色列表，用於驗證碼、噪線、噪點
            Color[] oColors ={
             Color.Black,
             Color.Red,
             Color.Blue,
             Color.Green,
             Color.Orange,
             Color.Brown,
             Color.Brown,
             Color.DarkBlue
            };
            //字體列表，用於驗證碼
            string[] oFontNames = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
            //驗證碼的字元集，去掉了一些容易混淆的字元
            char[] oCharacter = {
       '2','3','4','5','6','8','9',
       'A','B','C','D','E','F','G','H','J','K', 'L','M','N','P','R','S','T','W','X','Y'
      };
            var oRnd = new Random();
            Bitmap oBmp = null;
            Graphics oGraphics = null;
            var n1 = 0;
            var oPoint1 = default(Point);
            var oPoint2 = default(Point);
            string sFontName = null;
            Font oFont = null;
            var oColor = default(Color);

            //生成驗證碼字串
            for (n1 = 0; n1 <= codeLength - 1; n1++)
            {
                sCode += oCharacter[oRnd.Next(oCharacter.Length)];
            }

            oBmp = new Bitmap(width, height);
            oGraphics = Graphics.FromImage(oBmp);
            oGraphics.Clear(Color.White);
            try
            {
                for (n1 = 0; n1 <= 4; n1++)
                {
                    //畫噪線
                    oPoint1.X = oRnd.Next(width);
                    oPoint1.Y = oRnd.Next(height);
                    oPoint2.X = oRnd.Next(width);
                    oPoint2.Y = oRnd.Next(height);
                    oColor = oColors[oRnd.Next(oColors.Length)];
                    oGraphics.DrawLine(new Pen(oColor), oPoint1, oPoint2);
                }

                float spaceWith = 0, dotX = 0, dotY = 0;
                if (codeLength != 0)
                {
                    // ReSharper disable once PossibleLossOfFraction
                    spaceWith = (width - fontSize * codeLength - 10) / codeLength;
                }

                for (n1 = 0; n1 <= sCode.Length - 1; n1++)
                {
                    //畫驗證碼字串
                    sFontName = oFontNames[oRnd.Next(oFontNames.Length)];
                    oFont = new Font(sFontName, fontSize, FontStyle.Italic);
                    oColor = oColors[oRnd.Next(oColors.Length)];

                    dotY = (height - oFont.Height) / 2 + 2;//中心下移2像素
                    dotX = Convert.ToSingle(n1) * fontSize + (n1 + 1) * spaceWith;

                    oGraphics.DrawString(sCode[n1].ToString(), oFont, new SolidBrush(oColor), dotX, dotY);
                }

                for (var i = 0; i <= 30; i++)
                {
                    //畫噪點
                    var x = oRnd.Next(oBmp.Width);
                    var y = oRnd.Next(oBmp.Height);
                    var clr = oColors[oRnd.Next(oColors.Length)];
                    oBmp.SetPixel(x, y, clr);
                }

                code = sCode;
                //保存图片数据
                var stream = new MemoryStream();
                oBmp.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return stream.ToArray();
            }
            finally
            {
                oGraphics.Dispose();
            }
        }
    }
}
